{% extends 'admin/dashboard.html' %}

{% block content %}
<div class="card">
    <h2 class="text-xl font-bold mb-4">网站设置</h2>
    <form id="settings-form">
        <!-- 示例设置项，可按需扩展 -->
        <div class="mb-4">
            <label for="site_name" class="block text-sm font-medium text-gray-700">网站名称</label>
            <input type="text" id="site_name" name="site_name" class="mt-1 p-2 w-full border rounded-md">
        </div>
        
        <div class="mb-4">
            <label for="site_description" class="block text-sm font-medium text-gray-700">网站描述</label>
            <textarea id="site_description" name="site_description" class="mt-1 p-2 w-full border rounded-md" rows="4"></textarea>
        </div>
        
        <button type="button" onclick="saveSettings()" class="px-4 py-2 bg-primary text-white rounded hover:bg-primary/80">保存设置</button>
    </form>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 获取当前网站设置
        fetch('/admin/settings')
            .then(response => response.json())
            .then(data => {
                document.getElementById('site_name').value = data.site_name;
                document.getElementById('site_description').value = data.site_description;
            })
            .catch(error => console.error('获取网站设置失败:', error));
    });

    // 保存网站设置函数
    function saveSettings() {
        const form = document.getElementById('settings-form');
        const formData = new FormData(form);
        const settings = {
            site_name: formData.get('site_name'),
            site_description: formData.get('site_description')
        };

        fetch('/admin/settings', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(settings),
        })
        .then(response => {
            if (response.ok) {
                alert('网站设置保存成功');
            } else {
                alert('网站设置保存失败');
            }
        })
        .catch(error => console.error('保存网站设置失败:', error));
    }
</script>
{% endblock %}